<template>
  <el-card>
    <el-row :gutter="20" class="header">
      <el-col :span="8">
        <el-input placeholder="请输入文章标题" clearable v-model="queryForm.title" />
      </el-col>
      <el-col :span="8">
        <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
      </el-col>
    </el-row>

    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column prop="title" label="文章标题" />
      <el-table-column prop="content" label="内容" :show-overflow-tooltip="true" />
      <el-table-column prop="hits" label="点击量" width="100" />
      <el-table-column prop="publish_time" label="发布时间" width="180" />
      <el-table-column prop="review_status" label="审核状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.review_status === 1 ? 'success' : 'warning'">
            {{ row.review_status === 1 ? '已审核' : '待审核' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="is_recommend" label="推荐状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.is_recommend === 1 ? 'success' : 'info'">
            {{ row.is_recommend === 1 ? '已推荐' : '未推荐' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250">
        <template #default="{ row }">
          <el-button type="success" size="small" :icon="Check"
            @click="handleReview(row)" v-if="row.review_status === 0" />
          <el-button type="warning" size="small" :icon="Star"
            @click="handleRecommend(row)" />
          <el-button type="danger" size="small" :icon="Delete" @click="handleDelete(row)" />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:current-page="queryForm.pageNum"
      v-model:page-size="queryForm.pageSize"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </el-card>

</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search, Delete, Check, Star } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import articleApi from '@/api/article'

const loading = ref(false)
const total = ref(0)

const queryForm = ref({
  pageNum: 1,
  pageSize: 10,
  title: '',
  is_delete: 0
})

const tableData = ref([])

// 获取列表数据
const getList = async () => {
  try {
    loading.value = true
    const { data } = await articleApi.getArticles(queryForm.value)
    if (data) {
      if (Array.isArray(data)) {
        tableData.value = data
        total.value = data.length
      } else {
      ElMessage.error('获取文章列表失败')
      }
    } else {
      ElMessage.error(data.message || '获取列表失败')
    }
  } catch (error) {
    console.error('获取列表失败:', error)
    ElMessage.error('获取列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  queryForm.value.pageNum = 1
  getList()
}

// 审核
const handleReview = async (row) => {
  try {
    await ElMessageBox.confirm('确认审核通过该文章?', '提示', {
      type: 'warning'
    })
    console.log(row.article_id)
    const { data } = await articleApi.updateArticle(row.article_id, {
      review_status: 1
    })
    if (data) {
      ElMessage.success('审核成功')
      getList()
    }
  } catch (error) {
    console.error('审核失败:', error)
  }
}

// 推荐
const handleRecommend = async (row) => {
  try {
    const newStatus = row.is_recommend === 1 ? 0 : 1
    const { data } = await articleApi.updateArticle(row.article_id, {
      is_recommend: newStatus
    })
    if (data) {
      ElMessage.success(newStatus === 1 ? '已设为推荐' : '已取消推荐')
      getList()
    }
  } catch (error) {
    console.error('操作失败:', error)
  }
}

// 删除
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确认删除该文章?', '提示', {
      type: 'warning'
    })
    const { data } = await articleApi.deleteArticle(row.article_id)
    if (data) {
      ElMessage.success('删除成功')
      getList()
    }
  } catch (error) {
    console.error('删除失败:', error)
  }
}

// 分页
const handleSizeChange = (size) => {
  queryForm.value.pageNum = 1
  queryForm.value.pageSize = size
  getList()
}

const handleCurrentChange = (page) => {
  queryForm.value.pageNum = page
  getList()
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.header {
  padding: 16px;
  box-sizing: border-box;
}

:deep(.el-pagination) {
  margin-top: 16px;
  justify-content: center;
}

.el-tag {
  width: 65px;
  text-align: center;
}
</style>
